package me.bcloud.web.spring;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import me.bcloud.web.domain.JobConfig;
import me.bcloud.web.domain.OnlineCounter;
import me.bcloud.web.domain.WorkUser;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class Task {

	@Resource
	private Cache cache;

	@Scheduled(fixedRate = 600000)
	public void doGCWorkList() {

		Map<Integer, HashMap<Integer, WorkUser>> workmap = cache.getWorkMap();

		int count = 0;
		Set<Integer> jobIds = workmap.keySet();
		for (Integer jobId : jobIds) {
			Map<Integer, WorkUser> workusersmap = workmap.get(jobId);
			Collection<WorkUser> workusers = workusersmap.values();
			for (Iterator<WorkUser> it = workusers.iterator(); it.hasNext();) {
				WorkUser workUser = it.next();
				if (System.currentTimeMillis()
						- workUser.getWorktime().getTime() > 600000) {
					cache.getCacheMap().get(jobId).add(workUser.getUser());
					count++;
					it.remove();
				}
			}

		}
		System.out.println("====================start gc worklist " + count + "=============");

	}
	@Scheduled(fixedRate = 600000)
	public void doGCCounter() {
		OnlineCounter.reduce();
		System.out.println("====================online " + OnlineCounter.getOnline()
				+ "=============");
	}
	
	public void setCache(Cache cache) {
		this.cache = cache;
	}

	public Cache getCache() {
		return cache;
	}
}
